/*
 * LoginPanel.java
 *
 * Created on September 21, 2008, 4:15 PM
 */
package no.eirikb.bomberman.applet;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import no.eirikb.bomberman.server.Server;
import no.eirikb.bomberman.shared.User;
import no.eirikb.bomberman.shared.img.ImageDB;

/**
 *
 * @author  eirikb
 */
public class LoginPanel extends javax.swing.JPanel {

    private Bomberman bomberman;

    /** Creates new form LoginPanel */
    public LoginPanel(Bomberman bomberman2) {
        this.bomberman = bomberman2;
        initComponents();

        if (connect()) {
            infoLabel.setText("Connected!  Loading image database...");

            ImageDB idb;
            try {
                System.out.println(bomberman.getServer());
                idb = bomberman.getServer().getImageDB();
                idb.generateImages();
                bomberman.setIdb(idb);
            } catch (RemoteException ex) {
                Logger.getLogger(LoginPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            infoLabel.setVisible(false);

            nicknameLabel.setEnabled(true);
            nicknameField.setEnabled(true);
            nicknameButton.setEnabled(true);

            new Thread() {

                @Override
                public void run() {
                    while (getGraphics() == null) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(LoginPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    while (getGraphics() != null && isVisible()) {
                        try {
                            getGraphics().drawImage(bomberman.getIdb().getWelcome().getImage(),
                                    0, 0, getWidth(), (int) (getHeight() * 0.9), null);
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException ex) {
                            }
                        } catch (Exception e) {
                        }
                    }
                }
            }.start();
        } else {
            infoLabel.setText("Unable to establish a connection!");
        }
    }

    public boolean connect() {
        try {
            Server server = (Server) Naming.lookup(bomberman.getHOST());
            bomberman.setServer(server);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nicknameLabel = new javax.swing.JLabel();
        nicknameField = new javax.swing.JTextField();
        nicknameButton = new javax.swing.JButton();
        infoLabel = new javax.swing.JLabel();
        loginErrorLabel = new javax.swing.JLabel();

        setBackground(java.awt.Color.green);

        nicknameLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        nicknameLabel.setText("Nickname:"); // NOI18N
        nicknameLabel.setEnabled(false);

        nicknameField.setEnabled(false);
        nicknameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nicknameFieldActionPerformed(evt);
            }
        });

        nicknameButton.setText("OK"); // NOI18N
        nicknameButton.setEnabled(false);
        nicknameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nicknameButtonActionPerformed(evt);
            }
        });

        infoLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        infoLabel.setText("Connecting..."); // NOI18N

        loginErrorLabel.setForeground(java.awt.Color.red);
        loginErrorLabel.setText(" "); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 585, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(infoLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(249, 249, 249)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nicknameLabel)
                            .addComponent(loginErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(nicknameField, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nicknameButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 288, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(infoLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                .addComponent(nicknameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nicknameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nicknameButton)
                .addGap(3, 3, 3)
                .addComponent(loginErrorLabel)
                .addGap(36, 36, 36))
        );
    }// </editor-fold>//GEN-END:initComponents

private void nicknameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nicknameFieldActionPerformed
    nicknameButton.doClick();
}//GEN-LAST:event_nicknameFieldActionPerformed

private void nicknameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nicknameButtonActionPerformed
    nicknameButton.setEnabled(false);
    nicknameField.setEnabled(false);
    if (nicknameField.getText().length() > 0) {
        try {
            User user = new User(nicknameField.getText());
            Double hash;
            if ((hash = bomberman.getServer().registerUser(bomberman.getClient(), user)) != null) {
                bomberman.setUser(user);
                bomberman.setHash(hash);
                setVisible(false);
                getParent().remove(this);
                bomberman.loginOK();
            } else {
                nicknameField.setText("");
                loginErrorLabel.setText("Nickname already in use.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        loginErrorLabel.setText("Please enter a nickname.");
    }
    nicknameButton.setEnabled(true);
    nicknameField.setEnabled(true);
}//GEN-LAST:event_nicknameButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel infoLabel;
    private javax.swing.JLabel loginErrorLabel;
    private javax.swing.JButton nicknameButton;
    private javax.swing.JTextField nicknameField;
    private javax.swing.JLabel nicknameLabel;
    // End of variables declaration//GEN-END:variables
}
